using Godot;
using System;

namespace EnemyStateNode
{
public partial class Idle : Node,StateNode
{
	public Enemy enemy;

	//控制位于闲置状态的行为
	public Timer timer;

    public override void _Ready()
    {		
        enemy = GetNode<Enemy>("/root/Main/Enemy");
		
		timer = GetNode<Timer>("Timer");
		timer.OneShot = true;
		timer.Timeout+=OnTimerTimeout;
    }			

	public void OnTimerTimeout()
	{		
		int num_rand = (int)MyUtil.RangeRandom(1, 100);
		//可以设置阶段枚举代替
		if (enemy.Health > 5) {
			if (num_rand <= 25)//25%
			{
				if (!enemy.IsOnFloor())
					enemy.SwitchTo("Fall");
				else
					enemy.SwitchTo("Jump");
			}
			else if (num_rand <= 50) {//25%
				if (!enemy.IsOnFloor())
					enemy.SwitchTo("Fall");
				else
					enemy.SwitchTo("Run");
			}
			else if (num_rand <= 80) {//30%
				enemy.SwitchTo("Squat");
			}
			else if (num_rand <= 90) {
				enemy.SwitchTo("ThrowSilk");
			}
			else
				enemy.SwitchTo("ThrowSword");
		}
		else {
			if (num_rand <= 25)//25%
			{
				enemy.SwitchTo("ThrowBarb");
			}
			else if (num_rand <= 60) {//35%
				enemy.SwitchTo("ThrowSword");
			}
			else if (num_rand <= 70) {//10%
				enemy.SwitchTo("ThrowSilk");
			}
			else if (num_rand <= 90) {
				if (!enemy.IsOnFloor())
					enemy.SwitchTo("Fall");
				else
					enemy.SwitchTo("Jump");
			}
			else
				enemy.SwitchTo("Squat");
}
	}

	public void OnEnter()
	{
		enemy.GetNode<AnimatedSprite2D>("AnimaEnemy").Animation="idle";

		if(enemy.Health<=5)
			timer.WaitTime = MyUtil.RangeRandom(0.1f,0.5f);
		else
			timer.WaitTime = MyUtil.RangeRandom(0.5f,0.8f);

		timer.Start();
	}

	public void OnExit(){}
	
	public void OnUpdate(float delta)
	{
		if(enemy.Health<=0)
			enemy.SwitchTo("Dead");
	}
}

}

